package ctr.noteEleve;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JTable;

import jdbc.Connexion;
import metier.Eleve;
import metier.Matiere;
import metier.NoteEleve;
import metier.Stage;
import metier.jdbc.note.NoteEleveJdbc;
import vue.MenuControle;
import vue.commun.JPanJTable;
import vue.noteEleve.JPanSuppNoteEleve;
import configuration.Constante;
import configuration.MessageAppli;
import ctr.commun.CommunGest;
import ctr.util.DateUtil;

public class GestSuppNoteEleve implements ActionListener,ItemListener,MouseListener {
	
	private JPanSuppNoteEleve jPanSuppNoteEleve;
	private JPanJTable jPanJTable;

	public GestSuppNoteEleve(JPanSuppNoteEleve jPanSuppNoteEleve,JPanJTable jPanJTable) {
		this.jPanSuppNoteEleve = jPanSuppNoteEleve;
		this.jPanJTable = jPanJTable;
		
	}

	@Override
	public void itemStateChanged(ItemEvent e) {
		// TODO Auto-generated method stub
		if (e.getStateChange()==ItemEvent.SELECTED) {
			if (e.getSource()==this.jPanSuppNoteEleve.getJcb_Stage()) {
				Stage stage = (Stage) jPanSuppNoteEleve.getJcb_Stage().getSelectedItem(); 
				jPanSuppNoteEleve.setStage(stage);
				jPanSuppNoteEleve.setBl_visible(false);
				jPanSuppNoteEleve.setBl_visibleModifier(false);
				jPanSuppNoteEleve.setEleve(null);
				MenuControle.afficherPan(new JPanSuppNoteEleve("Supprimer une note", jPanSuppNoteEleve));
			}else if (e.getSource()==this.jPanSuppNoteEleve.getJcb_Matiere()) {
				Matiere matiere = (Matiere)jPanSuppNoteEleve.getJcb_Matiere().getSelectedItem();
				jPanSuppNoteEleve.setBl_visible(true);
				jPanSuppNoteEleve.setMatiere(matiere);
				MenuControle.afficherPan(new JPanSuppNoteEleve("Supprimer une note", jPanSuppNoteEleve));
			} 
			
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
	if (e.getSource() instanceof JButton) {
			
			try {
				//je recupere la note en float
				float float_note = Float.parseFloat(this.jPanSuppNoteEleve.getJtf_Note().getText());
				//je test la note
				if (float_note>20f || float_note<0f) {
					throw new Exception(MessageAppli.MSG_NOTE_MAUVAIS_FORMAT+float_note);
				}
				
				
				//je recupere l'id de l'eleve
				int int_idEleve = this.jPanSuppNoteEleve.getEleve().getIdEleve();
				
				//je recupere l id matiere
				Matiere matiere = (Matiere) this.jPanSuppNoteEleve.getJcb_Matiere().getSelectedItem();
				int int_idMatiere = matiere.getIdMatiere();
				
				
				
				//je recupere la date
				String str_date = this.jPanSuppNoteEleve.getJtf_Date().getText();
				//je test la date
				Date date_date = DateUtil.checkDate(Constante.FORMAT_DATE_SHORT,
						str_date);
				if (date_date == null) {
					throw new Exception(MessageAppli.MSG_DATE_MAUVAIS_FORMAT
							+ Constante.FORMAT_DATE_SHORT);
				}
				
				NoteEleve noteEleve = new NoteEleve(int_idEleve, int_idMatiere, date_date, float_note);
				int opt;
				opt = JOptionPane.showConfirmDialog(null,
						MessageAppli.MSG_NOTE_SUPP_QUEST,
						MessageAppli.MSG_TITRE_INFO,
						JOptionPane.YES_NO_OPTION);
				
				if (opt == JOptionPane.OK_OPTION) {
					if (NoteEleveJdbc.supprimer(Connexion.getConnexion(), noteEleve)) {
						MenuControle.afficherPan(new JPanSuppNoteEleve(
								"Modifier une note", new CommunGest().getListeStage().get(0)));
					}else {
						throw new Exception(MessageAppli.MSG_NOTE_CREATION_KO);
					}	
				} 
				
			} catch (Exception e2) {
				// TODO: handle exception
				e2.printStackTrace();
				JOptionPane.showMessageDialog(null, e2.getMessage(),
						MessageAppli.MSG_TITRE_INFO, JOptionPane.WARNING_MESSAGE);
			}
		}
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		
		JTable jPanTest = (JTable)e.getSource();
		
		
		
		if ("tabNotes".equals(jPanTest.getName())) {
			int index = this.jPanJTable.getJtable().getSelectedRow();
			NoteEleve noteEleve = new CommunGest().getListeNoteEleveByMatiere(jPanSuppNoteEleve.getEleve(),
					jPanSuppNoteEleve.getMatiere()).get(index);
			jPanSuppNoteEleve.setNoteEleve(noteEleve);
			jPanSuppNoteEleve.setBl_visibleModifier(true);	
			MenuControle.afficherPan(new JPanSuppNoteEleve("Supprimer une note", jPanSuppNoteEleve));
			
			
			
			
		} else {
			Stage stage = (Stage) jPanSuppNoteEleve.getJcb_Stage().getSelectedItem(); 
			int index = this.jPanJTable.getJtable().getSelectedRow();
			Eleve eleve = new CommunGest().getListeEleve(stage).get(index);
			jPanSuppNoteEleve.setEleve(eleve);
			jPanSuppNoteEleve.setBl_visible(true);	
			jPanSuppNoteEleve.setBl_visibleModifier(false);
			jPanSuppNoteEleve.setMatiere(new CommunGest().getListeMatiere().get(0));
			MenuControle.afficherPan(new JPanSuppNoteEleve("Supprimer une note", jPanSuppNoteEleve));
			
			
		}
		
	
		
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	
	



}
